package com.cm.caseManagement.controller;

import com.cm.caseManagement.annotation.Log;
import com.cm.caseManagement.core.controller.BaseController;
import com.cm.caseManagement.core.domain.AjaxResult;
import com.cm.caseManagement.core.page.TableDataInfo;
import com.cm.caseManagement.pojo.entity.CmSysUser;
import com.cm.caseManagement.pojo.enums.BusinessType;
import com.cm.caseManagement.service.ICmSysUserService;
import com.cm.caseManagement.utils.poi.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户Controller
 *
 * @author ruoyi
 * @date 2025-10-05
 */
@RestController
@RequestMapping("/caseManagement/user")
public class CmSysUserController extends BaseController
{
    @Autowired
    private ICmSysUserService cmSysUserService;

    /**
     * 查询用户列表
     */
    @PreAuthorize("@ss.hasPermi('caseManagement:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(CmSysUser cmSysUser)
    {
        startPage();
        List<CmSysUser> list = cmSysUserService.selectCmSysUserList(cmSysUser);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @PreAuthorize("@ss.hasPermi('caseManagement:user:export')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmSysUser cmSysUser)
    {
        List<CmSysUser> list = cmSysUserService.selectCmSysUserList(cmSysUser);
        ExcelUtil<CmSysUser> util = new ExcelUtil<CmSysUser>(CmSysUser.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('caseManagement:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cmSysUserService.selectCmSysUserById(id));
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('caseManagement:user:add')")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmSysUser cmSysUser)
    {
        return toAjax(cmSysUserService.insertCmSysUser(cmSysUser));
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('caseManagement:user:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmSysUser cmSysUser)
    {
        return toAjax(cmSysUserService.updateCmSysUser(cmSysUser));
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('caseManagement:user:remove')")
    @Log(title = "用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cmSysUserService.deleteCmSysUserByIds(ids));
    }
}
